#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib import request, error


class HtmlDownloader(object):
    def download(self, url):
        if not bool(url):
            return ''

        try:
            # 获取网页数据
            urlObj = request.Request(url)
            # 设置header
            urlObj.add_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36")
            # 获取数据
            reponse = request.urlopen(urlObj)
            # 如果成功返回页面数据
            return reponse.read() if reponse.getcode() == 200 else ''
        except error.URLError as e:
            print(u"网页下载失败")
            print(e.reason)